<?php


namespace app\datacollect\controller;


use app\common\lib\exception\ApiException;
use think\Controller;

class DataCollectAuth extends Controller
{

    protected $errermsg = '';

    public function _initialize()
    {
        parent::_initialize();

        if (!$this->isValid())
        {
            throw new ApiException($this->errermsg, 200, config('code.denied'));
        }
    }

    /**
     * 校验 token 和 项目是否能对应上
     * @return bool
     */
    private function isValid(): bool
    {
        $params = $this->request->param();
        if (isset($params['project']) && isset($params['token']))
        {//请求参数不为空
            $conf_sink = config('conf_sink');
            if ($params['project'] === $conf_sink['project'] && $params['token'] === $conf_sink['token'])
            {//项目名字和token都能对应上
                return true;
            }
        }
        $this->errermsg = '鉴权失败！';
        return false;
    }
}
